import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class SplashMouse extends Canvas implements Runnable
{

	private MenuManager mgr;
	private SplashMouseThread smt;
	private boolean up = false;
	private boolean down = false;
	private boolean left = false;
	private boolean right = false;
	private boolean leftclc = false;
	private boolean rightclc = false;
	private boolean scrollup = false;
	private boolean scrolldown = false;
	
	public SplashMouse(MenuManager mm, String url)
	{
		this.mgr = mm;
		this.smt = new SplashMouseThread(url, this);
		this.smt.connect();
	}
	
	public void run()
	{
		while(true)
		{
			if(up)
				smt.setData("u");
			if(down)
				smt.setData("d");
			if(left)
				smt.setData("l");
			if(right)
				smt.setData("r");
			if(leftclc)
			{
				smt.setData("x");
				leftclc = false;
			}
			if(rightclc)
			{
				smt.setData("y");
				rightclc = false;
			}
			if(scrollup)
				smt.setData("w");
			if(scrolldown)
				smt.setData("s");
				
			try
			{
				Thread.sleep(10);
			}
			catch(Exception e)
			{}
		}
	}

	protected void keyPressed(int keyCode)
	{
		switch(getGameAction(keyCode))
		{
			case Canvas.UP:
				up = true;
			break;
			case Canvas.DOWN:
				down = true;
			break;
			case Canvas.LEFT:
				left = true;
			break;
			case Canvas.RIGHT:
				right = true;
			break;
			case Canvas.FIRE:
				leftclc = true;
			break;
		}
		switch(keyCode)
		{
			case Canvas.KEY_NUM2:
				up = true;
			break;
			case Canvas.KEY_NUM8:
				down = true;
			break;
			case Canvas.KEY_NUM4:
				left = true;
			break;
			case Canvas.KEY_NUM6:
				right = true;
			break;
			case Canvas.KEY_NUM7:
				leftclc = true;
			break;
			case Canvas.KEY_NUM9:
				rightclc = true;
			break;
		}
	}
	
	protected void keyReleased(int keyCode)
	{
		switch(getGameAction(keyCode))
		{
			case Canvas.UP:
				up = false;
			return;
			case Canvas.DOWN:
				down = false;
			return;
			case Canvas.LEFT:
				left = false;
			return;
			case Canvas.RIGHT:
				right = false;
			return;
			case Canvas.FIRE:
				leftclc = false;
			return;
		}
		switch(keyCode)
		{
			case Canvas.KEY_NUM2:
				up = false;
			break;
			case Canvas.KEY_NUM8:
				down = false;
			break;
			case Canvas.KEY_NUM4:
				left = false;
			break;
			case Canvas.KEY_NUM6:
				right = false;
			break;
			case Canvas.KEY_NUM7:
				leftclc = false;
			break;
			case Canvas.KEY_NUM9:
				rightclc = false;
			break;
			default:
				mgr.itemSelected(0);
			break;
		}
	}

	
	public void paint(Graphics g)
	{
		int w = getWidth();
		int h = getHeight();
		int emh = h / 6;
		int emw = w / 5;
		
		g.setColor(mgr.getDefaultBGColor());
		g.fillRect(0,0, getWidth(), getHeight());
		
		g.setColor(0x00FFFFFF - g.getColor());

		g.drawString("{ pritisni 0 za izhod }", w / 2 , 6 * emh, Graphics.BASELINE | Graphics.HCENTER);
		int visinae = h / 9;
		int sirinae = w / 6;
		
		g.drawLine(w / 2, emh + visinae, w / 2, emh * 3);
		g.drawLine(w / 2 - emw, emh *  2 + visinae / 2, w / 2 + emw, emh * 2 + visinae / 2);
		
		g.drawArc(w / 2 - (sirinae / 2), emh, sirinae, visinae, 0, 360);
		g.drawArc(w / 2 + emw, 2 * emh, sirinae, visinae, 0, 360);
		g.drawArc(w / 2 - emw - sirinae, 2 * emh, sirinae, visinae, 0, 360);
		g.drawArc(w / 2 - (sirinae / 2), 3 * emh, sirinae, visinae, 0, 360);
		g.drawArc(w / 2 + emw, 4 * emh, sirinae, visinae, 0, 360);
		g.drawArc(w / 2 - emw - sirinae, 4 * emh, sirinae, visinae, 0, 360);

		g.drawString("2", w / 2, emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("8", w / 2, 3 * emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("4", w / 2 - emw - (sirinae / 2), 2 * emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("6", w / 2 + emw + (sirinae / 2), 2 * emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("7", w / 2 - emw - (sirinae / 2), 4 * emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("9", w / 2 + emw + (sirinae / 2), 4 * emh + (visinae / 2), Graphics.BASELINE | Graphics.HCENTER);
		
		g.drawString("{levi klik}", w / 2 - emw - (sirinae / 2), 5 * emh, Graphics.BASELINE | Graphics.HCENTER);
		g.drawString("{levi klik}", w / 2 + emw + (sirinae / 2), 5 * emh, Graphics.BASELINE | Graphics.HCENTER);

	}
}
